<template>
  <el-dialog
    title="新增员工"
    :visible.sync="showDialog"
    :close-on-click-modal="issShow"
    :show-close="issShow"
    @closed="closeDialog"
  >
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="username">
        <el-input v-model="ruleForm.username" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="ruleForm.mobile"
          type="password"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="ruleForm.departmentName"
          style="width:50%"
          placeholder="请选择部门"
          @focus="getDepartments"
        />
        <el-tree
          v-show="isTree"
          :data="departmentList"
          :props="defaultProps"
          @node-click="handleNodeClick"
        />
      </el-form-item>

      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="ruleForm.workNumber" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="ruleForm.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" required>
        <el-col :span="11">
          <el-form-item prop="timeOfEntry">
            <el-date-picker
              v-model="ruleForm.timeOfEntry"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="转正时间" required>
        <el-col :span="11">
          <el-form-item prop="correctionTime">
            <el-date-picker
              v-model="ruleForm.correctionTime"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showDialog = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employee'
import { getDepartmentList } from '@/api/department'
import { fromData } from '@/utils/index'
import EmployeeEnum from '@/api/constant/employees'

export default {
  name: 'AddDialog',
  data() {
    return {
      issShow: false,
      showDialog: false,
      isTree: false,
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            trigger: 'blur',
            message: '手机号格式有误'
          }
        ],
        departmentName: [
          { required: true, trigger: 'bulr', message: '请选择' }
        ],
        timeOfEntry: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        correctionTime: [
          {
            type: 'date',
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ]
      },
      departmentList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      hireType: EmployeeEnum.hireType
    }
  },
  methods: {
    // 确认的点击事件
    confirm() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          await addEmployee(this.ruleForm)
          this.$message.success('新增成功')
          this.$parent.getList()
          this.showDialog = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭弹窗的回调事件
    closeDialog() {
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {}
    },
    // 部门获得焦点
    async getDepartments() {
      const res = await getDepartmentList()
      const rootTree = fromData(res.depts, '')
      this.departmentList = rootTree
      this.isTree = true
    },
    // 节点点击事件
    handleNodeClick(data) {
      // 获取name 并隐藏
      this.ruleForm.departmentName = data.name
      this.isTree = false
    }
  }
}
</script>

<style></style>
